<?php

require FCPATH . '/application/controllers/frontend.php';

class News extends Frontend {

    public function __construct() {
	parent::__construct();
    }
 
    public function index(){
	$this->load->model(array('news_category_model', 'news_model'));
	$this->load->helper('text');
	$category = intval($this->uri->segment(3));
	
	$lang = $this->get_language();
	$columns = array(
	    'id','created_at',
	    array('title_'.$lang,'title'),
	    array('nl.name','cover_name'),
	    array('content_'.$lang,'content')
	);
	$orders = array(
		array('created_at','desc')
	);
	$conditions = array();
	if($category){
	    $conditions[] = array('where','category',$category);
	}
	
	$category_columns = array(
	    'id',array('name_'.$lang,'name')
	);
	
	$this->load_css('frontend_news_index');
	$this->load_js('frontend_news_index');
	$this->rend('news_index',array(
	    'news' => $this->news_model->records($columns , $conditions, $orders),
	    'categories' => $this->news_category_model->records($category_columns),
	    'current_category_id' => $category,
	    'current_category' => $this->news_category_model->records($category_columns,array(array('where','id',$category)))
	));
    }
    
    public function show(){
	$id = intval($this->uri->segment(3));
	$this->load_css('frontend_news_index');
	$this->load_js(array('lib','frontend_news_show'));
	$this->load->model(array('news_category_model', 'news_model'));
	$lang = $this->get_language();
	
	$category_columns = array(
	    'id',array('name_'.$lang,'name')
	);
	
	$columns = array(
	    'id', 'created_at', 'category',
	    array('title_'.$lang,'title'),
	    array('nl.name','cover_name'),
	    array('content_'.$lang,'content')
	);
	
	$conditions = array(
	    array('where','id',$id)
	);
		
	$records = $this->news_model->records($columns , $conditions);
	
	$this->rend('news_show',array(
	    'categories' => $this->news_category_model->records($category_columns),
	    'news' => $records[0],
	    'current_category' => $this->news_category_model->records($category_columns,array(array('where','id',$records[0]->category)))
	));
    }
}